<?php
namespace App\Utilitys;

class Logs
{
    /**
     * 日志
     */

    protected $logDir = 'logs/';

    public function __construct()
    {
    }

    /**
     * 上传日志
     */
    public function setUpload($detail)
    {
        $name = 'upload'; $nameZn = '上传';
        $jsonStr = json_encode($detail,JSON_UNESCAPED_UNICODE);
        $haomiao = microtime(); $hms = explode(" ",$haomiao);
        $hm = intval($hms[0] * 1000000); //取毫秒前几位
        $jsonStr = date('Y-m-d H:i:s',time()).'.'.$hm.'：'.$name.'--'.$nameZn.'：'.$jsonStr;
        //日志名称
        if (!file_exists($this->logDir)) {
            exec("cd /www/liujiu/public/"); exec("mkdir logs"); exec("cd logs");
        }
        $logName = $this->logDir.$name.'：'.date('Y-m-d',time()).'.log';
        if (!file_exists($logName)) {
            $d = '##'.$name.'：'.$nameZn.PHP_EOL;
            file_put_contents($logName,$d, FILE_APPEND);
        }
        return file_put_contents($logName,$jsonStr.PHP_EOL, FILE_APPEND);
    }

    /**
     * FFmpeg指令日志
     */
    public function setFFmpeg($detail)
    {
        $name = 'FFmpeg';
        $haomiao = microtime(); $hms = explode(" ",$haomiao);
        $hm = intval($hms[0] * 1000000); //取毫秒前几位
        $jsonStr = date('Y-m-d H:i:s',time()).'.'.$hm.'：'.$detail;
        //日志名称
        if (!file_exists($this->logDir)) {
            exec("cd /www/liujiu/public/"); exec("mkdir logs"); exec("cd logs");
        }
        $logName = $this->logDir.$name.'：'.date('Y-m-d',time()).'.log';
        if (!file_exists($logName)) {
            $d = '##'.$name.'指令：'.PHP_EOL;
            file_put_contents($logName,$d, FILE_APPEND);
        }
        return file_put_contents($logName,$jsonStr.PHP_EOL, FILE_APPEND);
    }

    /**
     * 获取列表
     */
    public function getFileIndex()
    {
        $uploads = [ 'title'=>'APP上传日志' ];
        $ffmpegs = [ 'title'=>'合成日志' ];
        $webVideoUploads = [ 'title'=>'WEB上传日志' ];
        $dirs = scandir($this->logDir);
        foreach ($dirs as $dir){
            if($dir!='.' && $dir!='..'){
                $logDir = $this->logDir.$dir;
                $sizex = filesize($logDir);
                $size = $this->getSizeLevel($sizex);
                $files = explode('：',$dir);
                if ($files[0]=='upload') {
                    $uploads['links'][] = [ 'link'=>$logDir, 'size'=>$size ];
                } else if ($files[0]=='ffmpeg') {
                    $ffmpegs['links'][] = [ 'link'=>$logDir, 'size'=>$size ];
                } else if ($files[0]=='webVideoUpload') {
                    $webVideoUploads['links'][] = [ 'link'=>$logDir, 'size'=>$size ];
                }
            }
        }
        $items = [ 'uploads'=>$uploads, 'ffmpegs'=>$ffmpegs, 'webVideoUploads'=>$webVideoUploads ];
        return $items;
    }

    /**
     * 获取内容
     */
    public function getFileOne($link)
    {
        if (!$link || !file_exists($link)) {
            return false;
        }
        $detail = file_get_contents($link);
        if (!$detail) { return array(); }
        $rst = explode("\n", $detail);
        return $rst ? array_reverse($rst) : array();
    }

    /**
     * 文件大小分级
     */
    public function getSizeLevel($size)
    {
        if ($size>1024 && $size<=1024*1024) {
            return round($size/1024,2) . 'KB';
        }
        else if ($size>1024*1024 && $size<=1024*1024*1024) {
            return round($size/1024/1024,2) . 'MB';
        }
        else if ($size>1024*1024*1024) {
            return round($size/1024/1024/1024,2) . 'GB';
        }
        else {
            return $size . 'B';
        }
    }
}